package com.xinqi.modules.discuss.discuss.controller.web;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.xinqi.common.core.utils.MessageUtils;
import com.xinqi.common.satoken.utils.LoginHelper;
import com.xinqi.common.base.enums.YesNoNumberEnum;
import com.xinqi.common.base.response.PageInfo;
import com.xinqi.common.base.response.Param;
import com.xinqi.common.base.response.R;
import com.xinqi.modules.discuss.discuss.dto.req.*;
import com.xinqi.modules.discuss.discuss.dto.rsp.CourseDiscussResultDTO;
import com.xinqi.modules.discuss.discuss.dto.rsp.CourseDiscussStatisticsResultDTO;
import com.xinqi.modules.discuss.discuss.dto.rsp.CourseDiscussStudentLogResultDTO;
import com.xinqi.modules.discuss.discuss.service.CourseDiscussService;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * WEB -- 课程讨论
 *
 * @author: yyzhen
 * @date: 2023/03/13
 */
@Tag(name = "Web-课程讨论", description = "课程讨论")
@RestController
@RequestMapping("/web/discuss")
@RequiredArgsConstructor
public class CourseDiscussWebController {

    private final CourseDiscussService courseDiscussService;

    /**
     * 添加讨论
     *
     * @Title:
     * @MethodName: insert
     * [dto]
     * @Return com.xinqi.common.base.response.R
     * @Exception
     * @Description:
     * @author: yyzhen
     * @date: 2023/3/14 12:01
     */
    @PostMapping("/add")
    public R<Void> insert(@Validated @RequestBody CourseDiscussCreateDTO dto) {
        dto.setOperationUserId(LoginHelper.getUserId());
        boolean flag = courseDiscussService.insert(dto);
        if (Objects.equals(YesNoNumberEnum.YES.getCode(), dto.getNowRelease())) {
            if (flag) {
                return R.ok(MessageUtils.message("course.release.message.success"));
            } else {
                return R.fail(MessageUtils.message("course.release.message.fail"));
            }
        } else {
            if (flag) {
                return R.ok(MessageUtils.message("course.discuss.add.success"));
            } else {
                return R.fail(MessageUtils.message("course.discuss.add.fail"));
            }
        }
    }

    /**
     * 编辑讨论
     *
     * @Title:
     * @MethodName: update
     * [dto]
     * @Return com.xinqi.common.base.response.R
     * @Exception
     * @Description:
     * @author: yyzhen
     * @date: 2023/3/14 15:31
     */
    @PostMapping("/edit")
    public R<Void> update(@Validated @RequestBody CourseDiscussUpdateDTO dto) {
        dto.setOperationUserId(LoginHelper.getUserId());
        return R.ok(courseDiscussService.updateById(dto));
    }

    /**
     * 讨论列表查询
     *
     * @Title:
     * @MethodName: page
     * [pageable, dto]
     * @Return com.xinqi.common.base.response.R<com.xinqi.common.base.response.PageInfo < com.xinqi.modules.discuss.discusss.dto.rsp.CourseDiscussResultDTO>>
     * @Exception
     * @Description:
     * @author: yyzhen
     * @date: 2023/3/14 16:31
     */
    @GetMapping("/page")
    public R<PageInfo<CourseDiscussResultDTO>> page(Param pageable, CourseDiscussQueryDTO dto) {
        dto.setUserId(LoginHelper.getUserId());
        return R.ok(courseDiscussService.selectPage(pageable, dto));
    }

    /**
     * 查看讨论详情（查看即已读）
     *
     * @Title:
     * @MethodName: info
     * [id]
     * @Return com.xinqi.common.base.response.R<com.xinqi.modules.discuss.discusss.dto.rsp.CourseDiscussResultDTO>
     * @Exception
     * @Description:
     * @author: yyzhen
     * @date: 2023/3/14 15:33
     */
    @GetMapping("/info/{id}")
    public R<CourseDiscussResultDTO> info(@PathVariable("id") Long id) {
        return R.ok(courseDiscussService.findByIdAndUserId(id, LoginHelper.getUserId()));
    }


    /**
     * 删除讨论
     *
     * @Title:
     * @MethodName: remove
     * [id]
     * @Return com.xinqi.common.base.response.R
     * @Exception
     * @Description:
     * @author: yyzhen
     * @date: 2023/3/14 15:34
     */
    @GetMapping("/remove/{id}")
    public R<Void> remove(@PathVariable("id") Long id) {
        if (courseDiscussService.removeById(id, LoginHelper.getUserId())) {
            return R.ok(MessageUtils.message("course.discuss.remove.success"));
        }
        return R.fail(MessageUtils.message("course.discuss.remove.fail"));
    }

    /**
     * 批量删除
     *
     * @Title:
     * @MethodName: batchRemove
     * [ids]
     * @Return com.xinqi.common.base.response.R
     * @Exception
     * @Description:
     * @author: yyzhen
     * @date: 2023/3/15 16:42
     */
    @PostMapping("/batchRemove")
    public R<Void> batchRemove(@RequestBody CourseDiscussDeleteDTO dto) {
        dto.setUserId(LoginHelper.getUserId());
        if (courseDiscussService.removeByIds(dto)) {
            return R.ok(MessageUtils.message("course.discuss.remove.success"));
        }
        return R.fail(MessageUtils.message("course.discuss.remove.fail"));
    }

    /**
     * 设为精华
     *
     * @Title:
     * @MethodName: isSupper
     * [id]
     * @Return com.xinqi.common.base.response.R
     * @Exception
     * @Description:
     * @author: yyzhen
     * @date: 2023/3/14 17:46
     */
    @GetMapping("/isSupper/{id}")
    public R<Void> isSupper(@PathVariable("id") Long id) {
        if (courseDiscussService.isSupper(id, LoginHelper.getUserId())) {
            return R.ok(MessageUtils.message("course.discuss.message.success"));
        }
        return R.fail(MessageUtils.message("course.discuss.message.fail"));
    }

    /**
     * 取消精华
     *
     * @Title:
     * @MethodName: cancelSuper
     * [id]
     * @Return com.xinqi.common.base.response.R
     * @Exception
     * @Description:
     * @author: yyzhen
     * @date: 2023/3/14 17:46
     */
    @GetMapping("/cancelSuper/{id}")
    public R<Void> cancelSuper(@PathVariable("id") Long id) {
        if (courseDiscussService.cancelSuper(id, LoginHelper.getUserId())) {
            return R.ok(MessageUtils.message("course.discuss.message.success"));
        }
        return R.fail(MessageUtils.message("course.discuss.message.fail"));
    }

    /**
     * 设为置顶
     *
     * @Title:
     * @MethodName: isTop
     * [id]
     * @Return com.xinqi.common.base.response.R
     * @Exception
     * @Description:
     * @author: yyzhen
     * @date: 2023/3/14 17:46
     */
    @GetMapping("/isTop/{id}")
    public R<Void> isTop(@PathVariable("id") Long id) {
        if (courseDiscussService.isTop(id, LoginHelper.getUserId())) {
            return R.ok(MessageUtils.message("course.discuss.message.success"));
        }
        return R.fail(MessageUtils.message("course.discuss.message.fail"));
    }

    /**
     * 取消置顶
     *
     * @Title:
     * @MethodName: cancelTop
     * [id]
     * @Return com.xinqi.common.base.response.R
     * @Exception
     * @Description:
     * @author: yyzhen
     * @date: 2023/3/14 17:47
     */
    @GetMapping("/cancelTop/{id}")
    public R<Void> cancelTop(@PathVariable("id") Long id) {
        if (courseDiscussService.cancelTop(id, LoginHelper.getUserId())) {
            return R.ok(MessageUtils.message("course.discuss.message.success"));
        }
        return R.fail(MessageUtils.message("course.discuss.message.fail"));
    }

    /**
     * 发布
     *
     * @Title:
     * @MethodName: isRelease
     * [id]
     * @Return com.xinqi.common.base.response.R
     * @Exception
     * @Description:
     * @author: yyzhen
     * @date: 2023/3/14 17:47
     */
    @GetMapping("/isRelease/{id}")
    public R<Void> isRelease(@PathVariable("id") Long id) {
        if (courseDiscussService.isRelease(id, LoginHelper.getUserId())) {
            return R.ok(MessageUtils.message("course.release.message.success"));
        }
        return R.fail(MessageUtils.message("course.release.message.fail"));
    }

    /**
     * 发布
     *
     * @Title:
     * @MethodName: isRelease
     * [id]
     * @Return com.xinqi.common.base.response.R
     * @Exception
     * @Description:
     * @author: yyzhen
     * @date: 2023/3/14 17:47
     */
    @GetMapping("/isRelease/{id}/v2")
    public R<Void> isReleaseV2(@PathVariable("id") Long id) {
        if (courseDiscussService.isReleaseV2(id, LoginHelper.getUserId())) {
            return R.ok(MessageUtils.message("course.release.message.success"));
        }
        return R.fail(MessageUtils.message("course.release.message.fail"));
    }

    /**
     * 已参与列表/未参与列表
     * @Title:
     * @MethodName: page
     * [pageable, dto]
     * @Return com.xinqi.common.base.response.R<com.xinqi.common.base.response.PageInfo < com.xinqi.modules.discuss.discusss.dto.rsp.CourseDiscussStudentLogResultDTO>>
     * @Exception
     * @Description:
     * @author: yyzhen
     * @date: 2023/3/16 14:59
     */
    @GetMapping("/participatePage")
    public R<PageInfo<CourseDiscussStudentLogResultDTO>> page(Param pageable, CourseDiscussStudentLogQueryDTO dto) {
        return R.ok(courseDiscussService.selectStudentLogPage(pageable, dto));
    }

    /**
     * 讨论统计
     *
     * @Title:
     * @MethodName: statisticsPage
     * [pageable, dto]
     * @Return com.xinqi.common.base.response.R<com.xinqi.common.base.response.PageInfo < com.xinqi.modules.discuss.discusss.dto.rsp.CourseDiscussStatisticsResultDTO>>
     * @Exception
     * @Description:
     * @author: yyzhen
     * @date: 2023/3/16 15:53
     */
    @GetMapping("/statisticsPage")
    public R<PageInfo<CourseDiscussStatisticsResultDTO>> statisticsPage(Param pageable, CourseDiscussStatisticsQueryDTO dto) {
        return R.ok(courseDiscussService.selectStatisticsPage(pageable, dto));
    }

    /**
     * 讨论下载
     *
     * @Title:
     * @MethodName: donwloadVideos
     * [dto]
     * @Return com.xinqi.common.base.response.R
     * @Exception
     * @Description:
     * @author: yyzhen
     * @date: 2023/3/20 16:57
     */
    @PostMapping("/download")
    public R<List<String>> download(@RequestBody CourseDiscussDownloadDto dto) {
        dto.setUserId(LoginHelper.getUserId());
        List<String> urls = courseDiscussService.download(dto);
        if (CollectionUtils.isEmpty(urls)) {
            return R.fail(MessageUtils.message("course.discuss.download.blank"));
        }
        return R.ok(urls);
    }

}
